package flexbars.controls.qrCodeClasses
{

public class ReedSolomonBlock
{
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	public function ReedSolomonBlock(totalBytes:int, dataBytes:int)
	{
		_totalBytes = totalBytes;
		_dataBytes  = dataBytes;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Constants
	//
	//--------------------------------------------------------------------------
	
	private static const BLOCK_TABLE:Array /* of Object */ =
	[
		{},
		{ // 1
			L : [ [1, 26, 19] ],
			M : [ [1, 26, 16] ],
			Q : [ [1, 26, 13] ],
			H : [ [1, 26,  9] ]
		},
		{ // 2
			L : [ [1, 44, 34] ],
			M : [ [1, 44, 28] ],
			Q : [ [1, 44, 22] ],
			H : [ [1, 44, 16] ]
		},
		{ // 3
			L : [ [1, 70, 55] ],
			M : [ [1, 70, 44] ],
			Q : [ [2, 35, 17] ],
			H : [ [2, 35, 13] ]
		},
		{ // 4
			L : [ [1, 100, 80] ],
			M : [ [2,  50, 32] ],
			Q : [ [2,  50, 24] ],
			H : [ [4,  25,  9] ]
		},
		{ // 5
			L : [ [1, 134, 108] ],
			M : [ [2,  67,  43] ],
			Q : [ [2, 33, 15], [2, 34, 16] ],
			H : [ [2, 33, 11], [2, 34, 12] ]
		},
		{ // 6
			L : [ [2, 86, 68] ],
			M : [ [4, 43, 27] ],
			Q : [ [4, 43, 19] ],
			H : [ [4, 43, 15] ]
		},
		{ // 7
			L : [ [2, 98, 78] ],
			M : [ [4, 49, 31] ],
			Q : [ [2, 32, 14], [4, 33, 15] ],
			H : [ [4, 39, 13], [1, 40, 14] ]
		},
		{ // 8
			L : [ [2, 121, 97] ],
			M : [ [2, 60, 38], [2, 61, 39] ],
			Q : [ [4, 40, 18], [2, 41, 19] ],
			H : [ [4, 40, 14], [2, 41, 15] ]
		},
		{ // 9
			L : [ [2, 146, 116] ],
			M : [ [3, 58, 36], [2, 59, 37] ],
			Q : [ [4, 36, 16], [4, 37, 17] ],
			H : [ [4, 36, 12], [4, 37, 13] ]
		},
		{ // 10
			L : [ [2, 86, 68], [2, 87, 69] ],
			M : [ [4, 69, 43], [1, 70, 44] ],
			Q : [ [6, 43, 19], [2, 44, 20] ],
			H : [ [6, 43, 15], [2, 44, 16] ]
		},
		{ // 11
			L : [ [4, 101, 81] ],
			M : [ [1, 80, 50], [4, 81, 51] ],
			Q : [ [4, 50, 22], [4, 51, 23] ],
			H : [ [3, 36, 12], [8, 37, 13] ]
		},
		{ // 12
			L : [ [2, 116, 92], [2, 117, 93] ],
			M : [ [6,  58, 36], [2,  59, 37] ],
			Q : [ [4,  46, 20], [6,  47, 21] ],
			H : [ [7,  42, 14], [4,  43, 15] ]
		},
		{ // 13
			L : [ [4, 133, 107] ],
			M : [ [ 8, 59, 37], [1, 60, 38] ],
			Q : [ [ 8, 44, 20], [4, 45, 21] ],
			H : [ [12, 33, 11], [4, 34, 12] ]
		},
		{ // 14
			L : [ [ 3, 145, 115], [1, 146, 116] ],
			M : [ [ 4,  64,  40], [5,  65,  41] ],
			Q : [ [11,  36,  16], [5,  37,  17] ],
			H : [ [11,  36,  12], [5,  37,  13] ]
		},
		{ // 15
			L : [ [5, 109, 87], [1, 110, 88] ],
			M : [ [5,  65, 41], [5,  66, 42] ],
			Q : [ [5,  54, 24], [7,  55, 25] ],
			H : [ [11, 36, 12] ]
		},
		{ // 16
			L : [ [ 5, 122, 98], [ 1, 123, 99] ],
			M : [ [ 7,  73, 45], [ 3,  74, 46] ],
			Q : [ [15,  43, 19], [ 2,  44, 20] ],
			H : [ [ 3,  45, 15], [13,  46, 16] ]
		},
		{ // 17
			L : [ [ 1, 135, 107], [ 5, 136, 108] ],
			M : [ [10,  74,  46], [ 1,  75,  47] ],
			Q : [ [ 1,  50,  22], [15,  51,  23] ],
			H : [ [ 2,  42,  14], [17,  43,  15] ]
		},
		{ // 18
			L : [ [ 5, 150, 120], [ 1, 151, 121] ],
			M : [ [ 9,  69,  43], [ 4,  70,  44] ],
			Q : [ [17,  50,  22], [ 1,  51,  23] ],
			H : [ [ 2,  42,  14], [19,  43,  15] ]
		},
		{ // 19
			L : [ [ 3, 141, 113], [ 4, 142, 114] ],
			M : [ [ 3,  70,  44], [11,  71,  45] ],
			Q : [ [17,  47,  21], [ 4,  48,  22] ],
			H : [ [ 9,  39,  13], [16,  40,  14] ]
		},
		{ // 20
			L : [ [3, 135, 107], [ 5, 136, 108] ],
			M : [ [3,  67,  41], [13,  68,  42] ],
			Q : [ [15, 54,  24], [ 5,  55,  25] ],
			H : [ [15, 43,  15], [10,  44,  16] ]
		},
		{ // 21
			L : [ [4, 144, 116], [4, 145, 117] ],
			M : [ [17, 68, 42] ],
			Q : [ [17, 50, 22], [6, 51, 23] ],
			H : [ [19, 46, 16], [6, 47, 17] ]
		},
		{ // 22
			L : [ [2, 139, 111], [7, 140, 112] ],
			M : [ [17, 74, 46] ],
			Q : [ [7, 54, 24], [16, 55, 25] ],
			H : [ [34, 37, 13] ]
		},
		{ // 23
			L : [ [ 4, 151, 121], [ 5, 152, 122] ],
			M : [ [ 4,  75,  47], [14,  76,  48] ],
			Q : [ [11,  54,  24], [14,  55,  25] ],
			H : [ [16,  45,  15], [14,  46,  16] ]
		},
		{ // 24
			L : [ [ 6, 147, 117], [ 4, 148, 118] ],
			M : [ [ 6,  73,  45], [14,  74,  46] ],
			Q : [ [11,  54,  24], [16,  55,  25] ],
			H : [ [30,  46,  16], [ 2,  47,  17] ]
		},
		{ // 25
			L : [ [ 8, 132, 106], [ 4, 133, 107] ],
			M : [ [ 8,  75,  47], [13,  76,  48] ],
			Q : [ [ 7,  54,  24], [22,  55,  25] ],
			H : [ [22,  45,  15], [13,  46,  16] ]
		},
		{ // 26
			L : [ [10, 142, 114], [2, 143, 115] ],
			M : [ [19,  74,  46], [4,  75,  47] ],
			Q : [ [28,  50,  22], [6,  51,  23] ],
			H : [ [33,  46,  16], [4,  47,  17] ]
		},
		{ // 27
			L : [ [ 8, 152, 122], [ 4, 153, 123] ],
			M : [ [22,  73,  45], [ 3,  74,  46] ],
			Q : [ [ 8,  53,  23], [26,  54,  24] ],
			H : [ [12,  45,  15], [28,  46,  16] ]
		},
		{ // 28
			L : [ [ 3, 147, 117], [10, 148, 118] ],
			M : [ [ 3,  73,  45], [23,  74,  46] ],
			Q : [ [ 4,  54,  24], [31,  55,  25] ],
			H : [ [11,  45,  15], [31,  46,  16] ]
		},
		{ // 29
			L : [ [ 7, 146, 116], [ 7, 147, 117] ],
			M : [ [21,  73,  45], [ 7,  74,  46] ],
			Q : [ [ 1,  53,  23], [37,  54,  24] ],
			H : [ [19,  45,  15], [26,  46,  16] ]
		},
		{ // 30
			L : [ [ 5, 145, 115], [10, 146, 116] ],
			M : [ [19,  75,  47], [10,  76,  48] ],
			Q : [ [15,  54,  24], [25,  55,  25] ],
			H : [ [23,  45,  15], [25,  46,  16] ]
		},
		{ // 31
			L : [ [13, 145, 115], [ 3, 146, 116] ],
			M : [ [ 2,  74,  46], [29,  75,  47] ],
			Q : [ [42,  54,  24], [ 1,  55,  25] ],
			H : [ [23,  45,  15], [28,  46,  16] ]
		},
		{ // 32
			L : [ [17, 145, 115] ],
			M : [ [10, 74, 46], [23, 75, 47] ],
			Q : [ [10, 54, 24], [35, 55, 25] ],
			H : [ [19, 45, 15], [35, 46, 16] ]
		},
		{ // 33
			L : [ [17, 145, 115], [ 1, 146, 116] ],
			M : [ [14,  74,  46], [21,  75,  47] ],
			Q : [ [29,  54,  24], [19,  55,  25] ],
			H : [ [11,  45,  15], [46,  46,  16] ]
		},
		{ // 34
			L : [ [13, 145, 115], [ 6, 146, 116] ],
			M : [ [14,  74,  46], [23,  75,  47] ],
			Q : [ [44,  54,  24], [ 7,  55,  25] ],
			H : [ [59,  46,  16], [ 1,  47,  17] ]
		},
		{ // 35
			L : [ [12, 151, 121], [ 7, 152, 122] ],
			M : [ [12,  75,  47], [26,  76,  48] ],
			Q : [ [39,  54,  24], [14,  55,  25] ],
			H : [ [22,  45,  15], [41,  46,  16] ]
		},
		{ // 36
			L : [ [ 6, 151, 121], [14, 152, 122] ],
			M : [ [ 6,  75,  47], [34,  76,  48] ],
			Q : [ [46,  54,  24], [10,  55,  25] ],
			H : [ [ 2,  45,  15], [64,  46,  16] ]
		},
		{ // 37
			L : [ [17, 152, 122], [ 4, 153, 123] ],
			M : [ [29,  74,  46], [14,  75,  47] ],
			Q : [ [49,  54,  24], [10,  55,  25] ],
			H : [ [24,  45,  15], [46,  46,  16] ]
		},
		{ // 38
			L : [ [ 4, 152, 122], [18, 153, 123] ],
			M : [ [13,  74,  46], [32,  75,  47] ],
			Q : [ [48,  54,  24], [14,  55,  25] ],
			H : [ [42,  45,  15], [32,  46,  16] ]
		},
		{ // 39
			L : [ [20, 147, 117], [ 4, 148, 118] ],
			M : [ [40,  75,  47], [ 7,  76,  48] ],
			Q : [ [43,  54,  24], [22,  55,  25] ],
			H : [ [10,  45,  15], [67,  46,  16] ]
		},
		{ // 40
			L : [ [19, 148, 118], [ 6, 149, 119] ],
			M : [ [18,  75,  47], [31,  76,  48] ],
			Q : [ [34,  54,  24], [34,  55,  25] ],
			H : [ [20,  45,  15], [61,  46,  16] ]
		}
	];
	
	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------
	
    //----------------------------------
    //  dataBytes (read-only)
    //----------------------------------
    
	private var _dataBytes:int;
	
	public function get dataBytes():int
	{
		return _dataBytes;
	}
	
    //----------------------------------
    //  totalBytes (read-only)
    //----------------------------------

	private var _totalBytes:int;
	
	public function get totalBytes():int
	{
		return _totalBytes;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	
    //----------------------------------
    //  getBlocks
    //----------------------------------

	public static function getBlocks(version:int, ecl:ErrorCorrectionLevel)
		:Array /* of ReedSolomonBlock */
	{
		if (version < 1 || version > 40)
			throw new ArgumentError("ReedSolomonBlock version");
		
		var blocks:Array /* of int */ = BLOCK_TABLE[version][String(ecl)];
		var result:Array /* of ReedSolomonBlock */ = new Array();
		
		var n:int = blocks.length;
		for (var i:int = 0; i < n; i++)
		{
			var count:int = blocks[i][0];
			var totalCount:int = blocks[i][1];
			var dataCount:int = blocks[i][2];
			
			for (var j:int = 0; j < count; j++)
			{
				result.push(new ReedSolomonBlock(totalCount, dataCount) );	
			}
		}
		
		return result;
	}
	
}

}